package com.byx.chatclient.presenter.user;

import android.os.CountDownTimer;

import com.byx.chatclient.contract.user.ForgetPwdTaskContract;
import com.byx.chatclient.entity.RegisterWay;
import com.byx.chatclient.entity.Result;
import com.byx.chatclient.factory.RetrofitFactory;
import com.byx.chatclient.http.ApiUrl;
import com.byx.chatclient.model.ForgetPwdModel;
import com.byx.chatclient.presenter.BasePresenter;
import com.byx.chatclient.utils.ValidateUtil;
import com.byx.chatclient.view.activity.user.ForgetPwdActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * ForgetPwdPresenter
 *
 * @author Superb
 * @date 2021/3/24 21:54
 */
public class ForgetPwdPresenter extends BasePresenter<ForgetPwdActivity> implements ForgetPwdTaskContract.Presenter {
    private ForgetPwdModel forgetPwdModel;

    public ForgetPwdPresenter() {
        forgetPwdModel = new ForgetPwdModel();
    }

    /**
     * 获取验证码
     */
    @Override
    public void getVerificationCode() {
        if (isActive()){
            String loginId = view.getAccount();
            if (loginId.isEmpty()){
                view.onError("账号不能为空！");
            }else {
                forgetPwdModel.getVerificationCode(loginId, new ForgetPwdTaskContract.Model.SimpleCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        view.setEmail(data);
                        view.onSendVerificationCodeSuccess();
                    }

                    @Override
                    public void onError(String errMsg) {
                        view.onError(errMsg);
                    }
                });
            }
        }

    }

    /**
     * 忘记密码
     */
    @Override
    public void forgetPwd() {
        if (isActive()){
            String loginId = view.getAccount();
            String password = view.getPassword();
            if (!ValidateUtil.isPasswordValid(password)){
                view.onError("密码不合要求");
            }else {
                forgetPwdModel.forgetPwd(loginId, ValidateUtil.encodeByMd5(password), new ForgetPwdTaskContract.Model.SimpleCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        view.onSuccess();
                    }

                    @Override
                    public void onError(String errMsg) {
                        view.onError(errMsg);
                    }
                });
            }
        }
    }


    /**
     * 验证码校验
     */
    @Override
    public void checkVerificationCode() {
        if (isActive()){
            String loginId = view.getAccount();
            String code = view.getVerificationCode();
            if (loginId.isEmpty()){
                view.onError("账号不能为空！");
            }else if (!ValidateUtil.isVerificationValid(code)) {
                view.onError("验证码必须是6位数字！");
            }else {
                forgetPwdModel.checkVerificationCode(view.getEmail(), view.getVerificationCode(), new ForgetPwdTaskContract.Model.SimpleCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        view.onCheckVerificationCodeSuccess();
                    }

                    @Override
                    public void onError(String errMsg) {
                        view.onError("验证码不正确！");
                    }
                });
            }
        }
    }
}
